package madixin.t205;

import java.util.HashMap;
import java.util.Map;

public class Solution {

    /**
     * 正反2个方向验证
     **/
    public boolean isIsomorphic(String s, String t) {
        if (s == null || t == null || s.length() != t.length()) {
            return false;
        }
        return isIsomorphicHelper(s, t) && isIsomorphicHelper(t, s);
    }

    public boolean isIsomorphicHelper(String s, String t) {
        Map<Character, Character> index = new HashMap<>();//映射表
        for (int i = 0; i < s.length(); i++) {
            char curS = s.charAt(i);
            char curT = t.charAt(i);
            if (index.containsKey(curS) && index.get(curS) != curT) {
                return false;
            } else {
                index.put(curS, curT);
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.isIsomorphic("egg", "add"));
        System.out.println(s.isIsomorphic("foo", "bar"));
        System.out.println(s.isIsomorphic("paper", "title"));
        System.out.println(s.isIsomorphic("ab", "aa"));
    }
}
